package expiring;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import java.util.HashMap;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


public class AgeDomain extends Thread{
	String domain = "";
	HashMap<String,String> ageDomain = new HashMap<String,String>();
	
	public AgeDomain(String domain,HashMap<String, String> ageDomain)
	{
		this.domain = domain;
		this.ageDomain = ageDomain;
		this.start();
	}
	public void run()
	{
		ageDomain.put(domain, "");
		try {
			String age = "";
			URL url = new URL("http://www.seologs.com/dns/domain-check.html");
	        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	        connection.setDoOutput(true);
	        connection.setRequestMethod("POST");
	        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
	        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
	        writer.write("query=" + domain);
	        writer.write("&output=cdt");
	        writer.close();
	        
	        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
	        	 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	        	 String content = "";
	        	 while(1==1)
	 			 {
	 				 String str = reader.readLine();
	 				 if(str==null) break;
	 				 content+=str;   				
	 			 }
	        	 Document doc = Jsoup.parse(content);
	        	 String r = doc.select("div[class=KonaBody]").select("p").text();
	        	 String[] ageSplit = r.split("(approximately)");
	        	 if(ageSplit.length ==2)
	        	 {
	        		 age = ageSplit[1].replaceAll("[):]+", "");
	        		 ageDomain.put(domain, age);
	        	 }
	        } else {
	            // Server returned HTTP error code.
	        }
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
